﻿using OOAD_PROJEKAT;
using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Main_application;
using System.Threading;
using Xceed.Wpf.Toolkit;
using System.ComponentModel;
using System.Windows.Threading;

namespace WpfApplication25
{
    /// <summary>
    /// Interaction logic for Zaposlenicka.xaml
    /// </summary>
    public partial class Zaposlenicka : Window
    {
        Window1 login;
        Zaposlenik x;
        public delegate void NextPrimeDelegate();
        List<Klijent> klija = new List<Klijent>();
        int boj = 0;
        DecimalUpDown cjena = new DecimalUpDown();
        DecimalUpDown pop = new DecimalUpDown();
        IntegerUpDown kol = new IntegerUpDown();
        public Zaposlenicka(Window1 ta, Zaposlenik na)
        {
            InitializeComponent();
            login = ta;
            x = na;
            this.Title = x.Funkcija;
            ab.BorderBrush = SystemColors.ControlDarkBrush;
            ac.BorderBrush = SystemColors.ControlDarkBrush;
            ad.BorderBrush = SystemColors.ControlDarkBrush;
            ae.BorderBrush = SystemColors.ControlDarkBrush;
            af.BorderBrush = SystemColors.ControlDarkBrush;

            
            cjena.Height = 20;
            cjena.Width = 120;
            cjena.Minimum = 0;
            cjena.Value = 0;
            cjena.Maximum = 10000;
            dodajPMreza.Children.Add(cjena);
            Grid.SetColumn(cjena, 1);
            Grid.SetRow(cjena, 0);
            
            pop.Height = 20;
            pop.Width = 120;
            pop.Minimum = 0;
            pop.Maximum = (decimal) 1;
            pop.Value = 0;
            pop.Increment = (decimal)0.1;
            dodajPMreza.Children.Add(pop);
            Grid.SetColumn(pop, 1);
            Grid.SetRow(pop, 1);

            kol.Height = 20;
            kol.Width = 120;
            kol.Minimum = 0;
            kol.Value = 0;
            kol.Maximum = 10000;
            zahtjeviMreza.Children.Add(kol);
            Grid.SetColumn(kol, 1);
            Grid.SetRow(kol, 0);

            ucitajP();
            

        }

        private void ucitajP()
        {
            Thread t = new Thread(new ThreadStart(delegate
            {
                this.Dispatcher.BeginInvoke((Action)delegate {
                    try
                    {
                        DAL.DAL d = DAL.DAL.Instanca;
                        d.kreirajKonekciju("localhost", "osiguranje", "root", "");
                        DAL.DAL.AutomobilDAO sd1 = d.getDAO.getAutomobilDAO();
                        List<Automobil> vid1 = sd1.getAll();

                        automobili.Items.Clear();
                        foreach (Automobil v in vid1)
                        {
                            automobili.Items.Add(v);
                        }

                        DAL.DAL.zahtjevDAO sd = d.getDAO.getZahtjevDAO();
                        List<zahtjevPolice> vid = sd.getAll();
                        zahtjevicombo.Items.Clear();
                        foreach (zahtjevPolice v in vid)
                        {
                            zahtjevicombo.Items.Add(v);
                        }


                    }
                    catch (Exception exx)
                    {
                        global::System.Windows.MessageBox.Show(exx.Message);
                    }     
                });

            }));
            t.IsBackground = true;
            t.Start();
        }

      

        #region Dodavanje klijenta
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DAL.DAL.KlijentDAO sama = new DAL.DAL.KlijentDAO();
            if (String.IsNullOrEmpty(ab.Text))
            {
                ab.BorderBrush = Brushes.Red;
                ab.ToolTip = "Morate unijeti ime klijenta!";
            }
            else
            {
                ab.BorderBrush = SystemColors.ControlDarkBrush;
                ab.ToolTip = null;
                if (String.IsNullOrEmpty(ac.Text))
                {
                    ac.BorderBrush = Brushes.Red;
                    ac.ToolTip = "Morate unijeti prezime klijenta!";
                }
                else
                {
                    ac.BorderBrush = SystemColors.ControlDarkBrush;
                    ac.ToolTip = null;
                    bool sveBrojevi = true;
                    for (int i = 0; i < ad.Text.Count(); i++)
                    {
                        if (Char.IsDigit(ad.Text[i]) == false)
                        {
                            sveBrojevi = false;
                            break;
                        }
                    }
                    if (String.IsNullOrEmpty(ad.Text) || ad.Text.Count() != 13 || sveBrojevi == false)
                    {
                        ad.BorderBrush = Brushes.Red;
                        ad.ToolTip = "Neispravan unos jmbg klijenta!";
                    }
                    else
                    {
                        ad.BorderBrush = SystemColors.ControlDarkBrush;
                        ad.ToolTip = null;
                        if (String.IsNullOrEmpty(kombo.Text))
                        {
                            kombo.BorderBrush = Brushes.Red;
                            kombo.ToolTip = "Morate izabrati status klijenta!";
                        }
                        else
                        {
                            kombo.BorderBrush = SystemColors.ControlDarkBrush;
                            kombo.ToolTip = "";
                            List<Klijent> klijenti = sama.getAll();
                            bool ispravan = true;
                            if (String.IsNullOrEmpty(ae.Text))
                                ispravan = false;
                            foreach (Klijent k in klijenti)
                            {
                                if (k.Username == ae.Text)
                                {
                                    ispravan = false;
                                    break;
                                }
                            }
                            if (ispravan == false)
                            {
                                ae.BorderBrush = Brushes.Red;
                                ae.ToolTip = "Username je već zauzet!";
                            }
                            else
                            {
                                ae.BorderBrush = SystemColors.ControlDarkBrush;
                                ae.ToolTip = null;
                                if (af.Text.Count() < 5)
                                {
                                    af.BorderBrush = Brushes.Red;
                                    af.ToolTip = "Password mora imati barem 4 karaktera!";
                                }
                                else
                                {
                                    af.BorderBrush = SystemColors.ControlDarkBrush;
                                    af.ToolTip = null;
                                    sama.create(new Klijent(ae.Text,ab.Text, ac.Text, ad.Text, af.Text, kombo.Text));
                                    System.Windows.MessageBox.Show("Uspješno ste dodali novog klijenta", "Klijent dodan", MessageBoxButton.OK, MessageBoxImage.Information);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void Dodaj_Policu_Click(object sender, RoutedEventArgs e)
        {
            bool prosao = true;
            DAL.DAL d = DAL.DAL.Instanca;
            d.kreirajKonekciju("localhost", "osiguranje", "root", "");
            DAL.DAL.PolicaDAO sd = d.getDAO.getPolicaDAO();
            DateTime? p = potpisivanje.SelectedDate;
            DateTime pot = new DateTime();
            if (p != null) pot = p.Value;
            else prosao = false;
            DateTime? p1 = potpisivanje.SelectedDate;
            DateTime pot1 = new DateTime();
            if (p != null) pot1 = p1.Value;
            else prosao = false;
            bool? b = evidentirana.IsChecked;
            bool b1 = true;
            if (b != null)
                b1 = b.Value;
            else prosao = false;

            if (automobili.SelectedIndex == -1) prosao = false;

            if (prosao)
            {
                sd.create(new PolicaOsiguranja(0, (double)cjena.Value, (double)cjena.Value, pot, pot1, b1, karakteristike.Text, (automobili.SelectedItem as Automobil).IdKlijenta, x.Id, (automobili.SelectedItem as Automobil).Id));
                global::System.Windows.MessageBox.Show("Dodali ste policu");
            }
            else
                global::System.Windows.MessageBox.Show("Popunite ispravno sve");
        }

        private void Posalji_Zahtjev_Click(object sender, RoutedEventArgs e)
        {
            DAL.DAL d = DAL.DAL.Instanca;
            d.kreirajKonekciju("localhost", "osiguranje", "root", "");
            DAL.DAL.zahtjevDAO sd = d.getDAO.getZahtjevDAO();

            int? koli = kol.Value;
            int kolicina = 0;
            if (koli != null && koli.Value > 0)
            {
                kolicina = koli.Value;

                sd.create(new zahtjevPolice(x.Id, kolicina));
                global::System.Windows.MessageBox.Show("Dodali ste zahtjev");
                
                List<zahtjevPolice> vid = sd.getAll();
                zahtjevicombo.Items.Clear();
                foreach (zahtjevPolice v in vid)
                {
                    zahtjevicombo.Items.Add(v);
                }
            }
            else
                global::System.Windows.MessageBox.Show("Unesite kolicinu");

            

        }

        private void Obrisi_Zahtjev_Click(object sender, RoutedEventArgs e)
        {
            DAL.DAL d = DAL.DAL.Instanca;
            d.kreirajKonekciju("localhost", "osiguranje", "root", "");
            DAL.DAL.zahtjevDAO sd = d.getDAO.getZahtjevDAO();

            if (zahtjevicombo.SelectedIndex == -1)
                global::System.Windows.MessageBox.Show("Odaberite zahtjev");
            else if ((zahtjevicombo.SelectedItem as zahtjevPolice).IdZaposlenika != x.Id)
                global::System.Windows.MessageBox.Show("Mozete obrisati samo svoje zahtjeve");
            else
            {
                zahtjevicombo.Items.RemoveAt(zahtjevicombo.SelectedIndex);
                global::System.Windows.MessageBox.Show("Obrisali ste zahtjev");
            }


        }


        #endregion

        #region Pretraga klijenata
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(ba.Text))
            {
                ba.BorderBrush = Brushes.Red;
                ba.ToolTip = "Niste unijeli podatak za pretragu!";
            }
            else
            {
                ba.BorderBrush = SystemColors.ControlDarkBrush;
                ba.ToolTip = null;
                bool postavljen = false;
                List<CheckBox> lista = new List<CheckBox>();
                napuniListu(lista);
                foreach (CheckBox sarma in lista)
                {
                    if (sarma.IsChecked == true)
                    {
                        postavljen = true;
                        break;
                    }
                }
                if (postavljen == false)
                {
                    System.Windows.MessageBox.Show("Morate izabrati parametar pretrage!", "Greška pretrage", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    prob.Children.Clear();
                    DAL.DAL.KlijentDAO klijeD = new DAL.DAL.KlijentDAO();
                    klija = klijeD.getAll();
                    boj = 0;
                    dugme.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NextPrimeDelegate(pretraga));
                }
            }
        }

        private void pretraga()
        {
            pretrazi();
            dugme.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle,
                    new NextPrimeDelegate(this.pretraga));

        }
        #endregion

        private void pretrazi()
        {
            if (boj != klija.Count)
            {
                Button testni = new Button();
                testni.Height = 50;
                testni.Width = 100;
                testni.Click += kliker;
                testni.Background = Brushes.White;
                List<CheckBox> ceki = new List<CheckBox>();
                napuniListu(ceki);
                string parametar = "";
                for (int i = 0; i < ceki.Count; i++)
                {
                    if (ceki[i].IsChecked == true)
                    {
                        parametar = ceki[i].Content.ToString();
                        break;
                    }
                }
                switch (parametar)
                    {
                        case "Ime":
                            if (klija[boj].Ime == ba.Text)
                            {
                                testni.ToolTip = klija[boj].Id;
                                testni.Content = klija[boj].Ime + " " + klija[boj].Prezime;
                                prob.Children.Add(testni);
                            }
                            break;
                        case "Prezime":
                            if (klija[boj].Prezime == ba.Text)
                            {
                                testni.ToolTip = klija[boj].Id;
                                testni.Content = klija[boj].Ime + " " + klija[boj].Prezime;
                                prob.Children.Add(testni);
                            }
                            break;
                        case "JMBG":
                            if (klija[boj].Jmbg == ba.Text)
                            {
                                testni.ToolTip = klija[boj].Id;
                                testni.Content = klija[boj].Ime + " " + klija[boj].Prezime;
                                prob.Children.Add(testni);
                            }
                            break;
                        case "Status":
                            if (klija[boj].statusKlijenta.ToString() == ba.Text)
                            {
                                testni.ToolTip = klija[boj].Id;
                                testni.Content = klija[boj].Ime + " " + klija[boj].Prezime;
                                prob.Children.Add(testni);
                            }
                            break;
                        case "Username":
                            if (klija[boj].Username == ba.Text)
                            {
                                testni.ToolTip = klija[boj].Id;
                                testni.Content = klija[boj].Ime + " " + klija[boj].Prezime;
                                prob.Children.Add(testni);
                            }
                            break;
                        case "ID klijenta":
                            if (klija[boj].Id.ToString() == ba.Text)
                            {
                                testni.ToolTip = klija[boj].Id;
                                testni.Content = klija[boj].Ime + " " + klija[boj].Prezime;
                                prob.Children.Add(testni);
                            }
                            break;
                }
                boj++;
            }
        }

        private void kliker(object sender, RoutedEventArgs e)
        {
            Button dugme = sender as Button;
            dugme.Background = Brushes.Red;
            dugme.Foreground = Brushes.White;
            dugme.FontWeight = FontWeights.Bold;
            foreach (Button x in prob.Children)
            {
                if (x.ToolTip.ToString() != dugme.ToolTip.ToString())
                {
                    x.Background = Brushes.White;
                    x.Foreground = Brushes.Black;
                    x.FontWeight = FontWeights.Normal;
                }
            }
        }            

        private void napuniListu(List<CheckBox> ma)
        {
            ma.Add(jed);
            ma.Add(dva);
            ma.Add(tri);
            ma.Add(cet);
            ma.Add(pet);
            ma.Add(sest);
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox probni = sender as CheckBox;
            List<CheckBox> lista = new List<CheckBox>();
            napuniListu(lista);
            foreach (CheckBox sarma in lista)
            {
                if (sarma.Content != probni.Content)
                    sarma.IsChecked = false;
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            foreach (Button b in prob.Children)
            {
                if (b.FontWeight == FontWeights.Bold)
                {
                    DAL.DAL.KlijentDAO ka = new DAL.DAL.KlijentDAO();
                    prob.Children.Remove(b);
                    for (int i = 0; i < klija.Count; i++)
                    {
                        if (klija[i].Id.ToString() == b.ToolTip.ToString())
                        {
                            ka.delete(klija[i]);
                            System.Windows.MessageBox.Show("Klijent uspješno izbrisan!", "Klijent izbrisan", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                }
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            int indeks = 0;
            foreach (Button b in prob.Children)
            {
                if (b.FontWeight == FontWeights.Bold)
                {
                    for (int i = 0; i < klija.Count; i++)
                    {
                        if (klija[i].Id.ToString() == b.ToolTip.ToString())
                        {
                            indeks = i;
                            break;
                        }
                    }
                }
            }
            promjena novo = new promjena(klija[indeks]);
            novo.ShowDialog();
        }
    }
}
